﻿

using System;
using System.Collections.Generic;
using System.Drawing;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace Controller {
    public class PngUtil {
        public static  int width = 380;
        public static  int high = 590;
        public static int gapTop = 15;//文字到顶端得距离
        public static int lines = 12;
        public static int marginLeft = 10;//边框
        public static int marginTop = 10;
        public static int qrSize = 80;//二维码大小
        public static int gap = (high - 2 * marginTop) / (lines - 1);//行高
        public static int contentWidth = (width - 2 * marginLeft);//行高
        public static Font f = new Font("宋体", 12, FontStyle.Bold);//正文文字
   
        public static Brush brush = new SolidBrush(Color.Black);//画刷    

        public static Bitmap CreateQRCode(string asset) {

            EncodingOptions options = new QrCodeEncodingOptions {
                DisableECI = true,
                CharacterSet = "UTF-8", //编码
                Width = PngUtil.qrSize,           //宽度
                Height = PngUtil.qrSize,           //高度
                Margin = 0
            };
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
            Bitmap old = writer.Write(asset);
            return old;

        }
        public static void ds(Graphics g, String info, Font f, Brush brush, float x, float y, String name) {
            if (dict.ContainsKey(name + "X")) {
                x = dict[name + "X"];
            }
            if (dict.ContainsKey(name + "Y")) {
                y = dict[name + "Y"];
            }
            ds(g, info, f, brush, x, y);
        }
        public static void ds(Graphics g, String info, Font f, Brush brush, float x, float y) {
            float size = f.Size;
            float len = 0;
            for (int i = 0; i < info.Length; i++) {
                if ((int)info[i] > 127) {
                    len += size * 2;
                } else {
                    len += size;
                }
            }
            g.DrawString(info, f, brush, x - len / 2 * 0.70f, y);
        }

        
       
        static Dictionary<String, int> dict = new Dictionary<string, int>();

        static PngUtil() {
            try {
                string[] lines = System.IO.File.ReadAllLines(@"set.ini");
                foreach (string line in lines) {
                    String[] kv = line.Split("=");
                    if (kv.Length == 2) {
                        dict.Add(kv[0], int.Parse(kv[1].Trim()));
                    }
                }
            } catch (Exception e) { 
            }

        }
        public static void drawStatic(Graphics g) {
         Font f = new Font("宋体", 10, FontStyle.Bold);//正文文字
            ds(g, "SINOCHEM", new Font("黑体", 19, FontStyle.Regular), brush, width / 2, 20);

            ds(g, "产品名称/Product name", f, brush, marginLeft + contentWidth / 4, marginTop + gap + gapTop);
            ds(g, "规格/Specification:", f, brush, marginLeft + contentWidth * 3 / 4, marginTop + gap + gapTop);

            ds(g, "对位芳纶长丝", f, brush, marginLeft + contentWidth / 4, marginTop + gap * 2 + gapTop);

            ds(g, "型号/Type", f, brush, marginLeft + contentWidth / 4, marginTop + gap * 3 + gapTop);
            ds(g, "批号/Lot No:", f, brush, marginLeft + contentWidth * 3 / 4, marginTop + gap * 3 + gapTop);

            ds(g, "毛重/Gross Wt:", f, brush, marginLeft + contentWidth / 6, marginTop + gap * 5 + gapTop);
            ds(g, "净重/Net Wt:", f, brush, marginLeft + contentWidth * 3 / 6, marginTop + gap * 5 + gapTop);
            ds(g, "卷数/RoI:", f, brush, marginLeft + contentWidth * 5 / 6, marginTop + gap * 5 + gapTop);

            ds(g, "中化高性能纤维材料有限公司", f, brush, marginLeft + contentWidth * 4/7, marginTop + gap * 9 + gapTop);
             Font fyy = new Font("宋体", 8, FontStyle.Regular);//正文文字
             ds(g, "Sinochem high performance fiber materials Co..Ltd", fyy, brush, width / 2, marginTop + gap * 10 + gapTop);
            using (var image = Image.FromFile("logo.png")) {
                Bitmap bitmap = new Bitmap(image);
                g.DrawImage(bitmap, marginLeft + 10, marginTop + gap * 9 + 5);
            }
        }


      
      public  static void drawLine(Graphics g) {
            Pen p = new Pen(Color.Black, 2);
            for (int i = 0; i < lines; i++) {
                if (i == 10 || i == 8) {
                    continue;
                }
                g.DrawLine(p, new Point(marginLeft, marginTop + i * gap), new Point(width - marginLeft, marginTop + i * gap));
            }
            g.DrawLine(p, new Point(marginLeft, marginTop), new Point(marginLeft, marginTop + (lines - 1) * gap));
            g.DrawLine(p, new Point(width - marginLeft, marginTop), new Point(width - marginLeft, marginTop + (lines - 1) * gap));

            g.DrawLine(p, new Point(width / 2, marginTop + gap), new Point(width / 2, marginTop + 5 * gap));

            g.DrawLine(p, new Point(marginLeft + (width - 2 * marginLeft) / 3, marginTop + 5 * gap), new Point(marginLeft + (width - 2 * marginLeft) / 3, marginTop + 7 * gap));
            g.DrawLine(p, new Point(marginLeft + (width - 2 * marginLeft) * 2 / 3, marginTop + 5 * gap), new Point(marginLeft + (width - 2 * marginLeft) * 2 / 3, marginTop + 7 * gap));
        }


    }
}
